package com.kinglone.service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kinglone.dao.CommBookDao;
import com.kinglone.model.CommBookModel;
import com.kinglone.util.DateUtils;
import com.kinglone.util.PDFutils;
import com.kinglone.util.StringUtil;

import common.Logger;

@Service("commBookService")
public class CommBookService{
	
	private static final Logger logger = Logger.getLogger(CommBookService.class);

	@Autowired
	private CommBookDao commBookDao;
	
	
	

	

	public CommBookModel queryById(String id) {
		CommBookModel commBookModel = new CommBookModel();
		
		  commBookModel.setCommbook_cod("（SMZJ）质监特令〔2014〕第1号");
		  commBookModel.setUnt_name("三明市三元区华都商务宾馆");
		  commBookModel.setOverhaul_date("2018-03-22");
		  commBookModel.setProblem_memo("1、使用无证人员：压力容器、厂内机动车辆未配置安全管理人员。    2、压力容器未进行年度检查。");
		  commBookModel.setViolate_memo("《特种设备安全法》第十四条、第四十二条、第六十九条第三款");
		  commBookModel.setDeal_basis1_memo("《特种设备安全法》第八十四条第（二）项、第八十三条第（六）项、第八十六条第（二）项");
		  commBookModel.setOverhaul_demand_memo("1、制定特种设备事故应急专项预案，，并定期进行应急演练，逾期未改正则应停止使用有关特种设备。      2、责令进行压力容器年度检查。");
		  commBookModel.setRecon_jcunt_name("荆州市质量技术监督局");
		  commBookModel.setRecon_fyunt_name("松滋市人民政府");
		  commBookModel.setLaw_unt_name("松滋市人民法院");	
		
		return commBookModel;	
	//	return commBookDao.queryById(id);
	}

	public String createPDF(HttpServletRequest request, CommBookModel commbookInfo) {
		Map<String,Object> dataMap = new HashMap<String,Object>();
		
		//CommBookModel commbookInfo = commBookDao.queryById(TASK_ID);
		//文件保存路径
		String filePath = "D:/upload/pdf";
		//pdf模板
        String pdfTemplate = "PDFTemplate.pdf";
        //后缀
        String suffix = ".pdf";
      //文件保存名称
    	String fileName = "监察指令书"+DateUtils.format(new Date(), "yyyyMMddHHmmss");    	
        dataMap.put("commbook_cod", commbookInfo.getCommbook_cod());
        dataMap.put("unt_name", commbookInfo.getUnt_name());
       // dataMap.put("problem_memo", commbookInfo.getProblem_memo());
       
        String dataOne =" 　经检查，你单位（人）在特种设备安全方面存在下列问题：\n\t";
        String dataTwo =" 　上述问题违反了";
        String dataThree ="的规定，根据";
        String dataFour ="的规定，责令你单位（个人）于";
        String dataFive = "年";
        String dataSix = "月";
        String dataSeven ="日前采取以下措施予以改正或者消除事故隐患：\n\t";
        String dataEight ="  如对本指令书不服，可在接到指令书之日起60日之内向";
        String dataNine = "或";
        String dataTen = "申请行政复议，或者于6个月内向";
        String dataEleven = "提起行政诉讼。复议和起诉期间，不得停止改正或者停止消除事故隐患。\n\t \n\t";
        String dayaTwelve="  特种设备安全监察员签名:\n\t";
        String dataThirteen ="  被检查单位(负责人)签名：\n\t \n\t";
        String dataFourteen="            （特种设备监督管理部门公章或者安全监察专用章）\n\t";
        String dataYear = "                               年       月";
      //  dataMap.put("dataTemp", dataTwo+commbookInfo.getViolate_memo()+dataTwo+commbookInfo.getDeal_basis1_memo());
        String dataTemp = commbookInfo.getOverhaul_date();//2017-01-05 00:00:00.0
        System.out.println("dataTemp:"+dataTemp);
        String year = "  ";
        String month ="  ";
        String day ="  ";
        if(StringUtil.isNotEmpty(dataTemp)){
        	String [] str=dataTemp.split("-");
    		year = str[0];
    		month=str[1];
    		String dayTemp = str[2];
    		day = dayTemp.substring(0, 2);	
        }
        
       // dataMap.put("year",year);
       // dataMap.put("month", month);
       // dataMap.put("day", day);
       // dataMap.put("overhaul_demand_memo", commbookInfo.getOverhaul_demand_memo());
       // dataMap.put("recon_jcunt_name", commbookInfo.getRecon_jcunt_name());
        //dataMap.put("recon_fyunt_name", commbookInfo.getRecon_fyunt_name());
       // dataMap.put("law_unt_name", commbookInfo.getLaw_unt_name());
		dataMap.put("dataTemp",dataOne+commbookInfo.getProblem_memo()+"\n\t"+dataTwo+commbookInfo.getViolate_memo()+dataThree
				+commbookInfo.getDeal_basis1_memo()+dataFour+year+dataFive+month+dataSix+day+dataSeven+commbookInfo.getOverhaul_demand_memo()+"\n\t"
				+dataEight+commbookInfo.getRecon_jcunt_name()+dataNine+commbookInfo.getRecon_fyunt_name()+dataTen+commbookInfo.getLaw_unt_name()
				+dataEleven+dayaTwelve+dataThirteen+dataFourteen+dataYear);
    	PDFutils.createPDF(request,pdfTemplate,filePath, fileName, dataMap,suffix);
        
		return filePath+File.separator+fileName+suffix;
	}




}
